package com.useeinfo.asg.open.admin;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.logging.biz.AddressBiz;
import com.useeinfo.asg.modules.logging.entity.Address;
import com.useeinfo.asg.modules.security.biz.SystemAuthorityBiz;
import com.useeinfo.asg.modules.security.biz.SystemRoleBiz;
import com.useeinfo.asg.modules.security.biz.SystemUserBiz;
import com.useeinfo.asg.modules.security.entity.SystemAuthority;
import com.useeinfo.asg.modules.security.entity.SystemRole;
import com.useeinfo.asg.modules.security.entity.SystemUser;
import com.useeinfo.framework.extend.action.BaseAction;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by THINK on 2017/6/6.
 */
@Controller
@RequestMapping("/admin/user/create")
public class UserCreateAdminManageAction extends BaseAction {

    @Autowired
    private AddressBiz addressBiz;
    @Autowired
    @Qualifier("systemUserBizWeb")
    private SystemUserBiz userBiz;
    @Autowired
    @Qualifier("systemRoleBizWeb")
    private SystemRoleBiz roleBiz;
    @Autowired
    @Qualifier("systemAuthorityBizWeb")
    private SystemAuthorityBiz authorityBiz;

    @RequestMapping("/billion")
    @ResponseBody
    public JSONObject createBillionProjectUser() {

        JSONObject ret = CommUtil.jsonResult(false);
        List<SystemUser> userList = new ArrayList<SystemUser>();
        SystemUser user = null;
        String sql = "select id, addr_name, `level`, SUBSTR(addr_code,1,9), addr_code from address where delete_status = 0 and `level` = 4 and addr_code is NOT NULL;";
        List<Object> objectList = addressBiz.queryListByNative(sql);
        if(CollectionUtils.isNotEmpty(objectList)) {
            // 查出所有街道
            Map<Long, Address> addrMap = new HashMap<Long, Address>();
            List<Address> addrList = addressBiz.queryObjects("select o from Address o where o.deleteStatus = false and o.level = 4 and o.addrCode is not null", null);
            if(CollectionUtils.isNotEmpty(addrList)) {
                for(Address address : addrList) {
                    addrMap.put(address.getId(), address);
                }
            }
            if(addrMap.size() > 0) {
                for(Object o : objectList) {
                    Object[] objects = (Object[]) o;
                    user = new SystemUser();
                    long id = CommUtil.null2Long(objects[0]);
                    String account = CommUtil.null2String(objects[3]);
                    user.setCreateTime(new Date());
                    user.setDepartmentMark(2);   // 统计局
                    user.setUserAccount(account);
                    user.setUserName(account);
                    user.setUserPassword("123456");
                    user.setAddress(addrMap.get(id));       // 街道统计员
                    userList.add(user);
                }
            }
        }

        JSONArray array = new JSONArray();
        for (SystemUser tmpUser : userList) {
            if (userBiz.existUserAccount(tmpUser)) {
                JSONObject json = new JSONObject();
                json.put("account", tmpUser.getUserAccount());
                array.add(json);
            }else {
                userBiz.save(tmpUser);
            }
        }
        ret = CommUtil.jsonResult(true);
        ret.put("contents", array);

        return ret;
    }
}
